package cn.iocoder.yudao.module.famous.doctor.controller.admin.extraction;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.extraction.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.extraction.ExtractionDO;
import cn.iocoder.yudao.module.famous.doctor.convert.extraction.ExtractionConvert;
import cn.iocoder.yudao.module.famous.doctor.service.extraction.ExtractionService;

@Tag(name = "管理后台 - 开方")
@RestController
@RequestMapping("/famous.doctor/extraction")
@Validated
public class ExtractionController {

    @Resource
    private ExtractionService extractionService;

    @PostMapping("/create")
    @Operation(summary = "创建开方")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:create')")
    public CommonResult<Long> createExtraction(@Valid @RequestBody ExtractionCreateReqVO createReqVO) {
        return success(extractionService.createExtraction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新开方")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:update')")
    public CommonResult<Boolean> updateExtraction(@Valid @RequestBody ExtractionUpdateReqVO updateReqVO) {
        extractionService.updateExtraction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除开方")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:delete')")
    public CommonResult<Boolean> deleteExtraction(@RequestParam("id") Long id) {
        extractionService.deleteExtraction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得开方")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:query')")
    public CommonResult<ExtractionRespVO> getExtraction(@RequestParam("id") Long id) {
        ExtractionDO extraction = extractionService.getExtraction(id);
        return success(ExtractionConvert.INSTANCE.convert(extraction));
    }

    @GetMapping("/list")
    @Operation(summary = "获得开方列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:query')")
    public CommonResult<List<ExtractionRespVO>> getExtractionList(@RequestParam("ids") Collection<Long> ids) {
        List<ExtractionDO> list = extractionService.getExtractionList(ids);
        return success(ExtractionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得开方分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:query')")
    public CommonResult<PageResult<ExtractionRespVO>> getExtractionPage(@Valid ExtractionPageReqVO pageVO) {
        PageResult<ExtractionDO> pageResult = extractionService.getExtractionPage(pageVO);
        return success(ExtractionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出开方 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:extraction:export')")
    @OperateLog(type = EXPORT)
    public void exportExtractionExcel(@Valid ExtractionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ExtractionDO> list = extractionService.getExtractionList(exportReqVO);
        // 导出 Excel
        List<ExtractionExcelVO> datas = ExtractionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "开方.xls", "数据", ExtractionExcelVO.class, datas);
    }

}
